package com.tnxst.phexamination.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.tnxst.phexamination.R;
import com.tnxst.phexamination.dto.ServicePackageDTO;
import com.tnxst.phexamination.dto.SignDTO;
import com.tnxst.phexamination.fragment.PackageServiceFragment;
import com.tnxst.phexamination.fragment.SignFragment;
import com.tnxst.phexamination.utils.StringUtils;

import java.util.List;

public class SignServiceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;

    private FragmentManager fragmentManager;

    private List<SignDTO> signDTOS;

    private SignFragment fragment;


    public SignServiceAdapter(List<SignDTO> signDTOS, FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
        this.signDTOS = signDTOS;
    }

    public void setSignDTOS(List<SignDTO> signDTOS){
        this.signDTOS = signDTOS;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        View view =  LayoutInflater.from(context).inflate(R.layout.sign_management_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        ViewHolder holders = (ViewHolder) holder;
        SignDTO signDTO = signDTOS.get(position);

        if (StringUtils.isNotNull(signDTO.getPERSONNAME()))
            holders.name.setText(signDTO.getPERSONNAME());

        if (StringUtils.isNotNull(signDTO.getTEAM_EMP_NAME()))
            holders.doctorName.setText(signDTO.getTEAM_EMP_NAME());

        if (StringUtils.isNotNull(signDTO.getPHONETEL()))
            holders.phone.setText(signDTO.getPHONETEL());

        if (StringUtils.isNotNull(signDTO.getSIGNDATE()))
            holders.signDate.setText(signDTO.getSIGNDATE());

        if (StringUtils.isNotNull(signDTO.getENDTIME()))
            holders.overDate.setText(signDTO.getENDTIME());

        if (StringUtils.isNotNull(signDTO.getTAGS()))
            holders.tags.setText(signDTO.getTAGS());

        if (StringUtils.isNotNull(signDTO.getUpload()) && signDTO.getUpload() == false){
            holders.upload.setVisibility(View.VISIBLE);
        }

        holders.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showFragment(signDTO);
            }
        });
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView name;

        TextView doctorName;

        TextView phone;

        TextView signDate;

        TextView overDate;

        TextView tags;

        TextView upload;

        View view;

        public ViewHolder(View view) {
            super(view);
            name = view.findViewById(R.id.textView166);
            doctorName = view.findViewById(R.id.textView168);
            phone = view.findViewById(R.id.textView170);
            signDate = view.findViewById(R.id.textView171);
            overDate = view.findViewById(R.id.textView173);
            tags = view.findViewById(R.id.textView175);
            upload = view.findViewById(R.id.textView190);
            this.view = view;
        }
    }

    @Override
    public int getItemCount() {
        return signDTOS.size();
    }


    private void showFragment(SignDTO signDTO) {
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.setCustomAnimations(R.anim.fragment_slide_up, 0);
        fragment = SignFragment.newInstance();
        Bundle bundle = new Bundle();
        bundle.putSerializable("signDTO", signDTO);
        fragment.setArguments(bundle);
        ft.replace(android.R.id.content, fragment);
        ft.show(fragment);
        ft.commitAllowingStateLoss();
    }

}
